var path = require('path');
var rimraf = require('rimraf');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");


const OUTPUT_PATH = path.resolve(__dirname, './dist');

var config = {
  entry: {
    app: ['./src/index.js']
  },
  module: {
    rules: [
      // the 'transform-runtime' plugin tells babel to require the runtime
      // instead of inlining it.
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
        }
      },
      {
        test: /\.less$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: [
            { loader: 'css-loader', options: { importLoaders: 1 } },
            { loader: 'postcss-loader' }, 'less-loader']
        })
      }
    ],
  },
  output: {
    path: OUTPUT_PATH,
    filename: '[name].[chunkhash].js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'H5 template',
      template: 'templates/index.ejs'
    }),
    new ExtractTextPlugin("[name].[chunkhash].css"),
    new UglifyJsPlugin(),
  ]
}

if (process.env.NODE_ENV === 'develop') {
  config.devtool = "#eval-source-map";
} else if (process.env.NODE_ENV === 'production') {
  rimraf(OUTPUT_PATH, function () { console.log('Remove dist done!'); });
}

module.exports = config;